import qs from 'querystring'
import { getLijiTpl } from '@/views/tpl'
import { http } from '@/utils/axios'
import { getCurWebsite } from '@/views/common'

export default {
  methods: {
    getLijiItemData (item, index) {
      this.loading = true
      this.$set(this.accountList[index], 'statusText', '请求中...')
      this._getLijiItemData(item)
        .then(res => {
          console.log(res)
          if (res) {
            let { name } = item
            if (this.startDate && this.endDate) {
              name = this.startDate.substring(5) + '-' + this.endDate.substring(5) + '——' + name
            }
            const data = res
            const tplData = getLijiTpl({ data })
            this.sendToMainProcess(`${name}.html`, tplData)
          }
          this.$set(this.accountList[index], 'statusText', '请求成功')
        })
        .catch(e => {
          this.$set(this.accountList[index], 'statusText', e || '请求失败')
        })
        .finally(() => {
          this.loading = false
        })
    },
    getAllLijiItemData (reGetList) {
      this.loading = true
      const list = reGetList || this.accountList
      let tempData = []
      const len = list.length
      let index = 0
      const callback = () => {
        if (this.waitExportList.length === 0) {
          return this.$message.info('请求数据为空')
        }
        let name = this.accountList.map(e => e.name).join('、')
        if (this.startDate && this.endDate) {
          name = this.startDate.substring(5) + '-' + this.endDate.substring(5) + '——' + name
        }
        const tplData = getLijiTpl({ data: this.waitExportList })
        this.sendToMainProcess(`${name}.html`, tplData)

        this.loading = false
      }
      for (let i = 0; i < len; i++) {
        const item = list[i]
        const itemIndex = this.accountList.findIndex(a => a.name === item.name)
        this.$set(this.accountList[itemIndex], 'statusText', '请求中...')
        this._getLijiItemData(item)
          .then(data => {
            tempData[i] = [...data]
            this.$set(this.accountList[itemIndex], 'statusText', '请求成功')
          })
          .catch(e => {
            console.error(e)
            this.errorList.push(item)
            this.$set(this.accountList[itemIndex], 'statusText', e && typeof e === 'string' ? e : '请求失败')
          })
          .finally(() => {
            index = index + 1
            if (index === len) {
              console.log(tempData)
              tempData = tempData.filter(e => e)
              tempData.forEach(item => {
                this.waitExportList.push(...item)
              })
              this.loading = false
              this.checkExistErrorList(() => callback())
            }
          })
      }
    },
    _getLijiItemData (item) {
      return new Promise((resolve, reject) => {
        const { name, psw, code } = item
        const children = this.getChilren(item.children)
        const { startDate, endDate } = this
        const baseUrl = getCurWebsite()
        const url = baseUrl + '/liji/gen'
        // eslint-disable-next-line eqeqeq
        let data
        if (children && children.length) {
          data = qs.stringify({
            username: name,
            pwd: psw,
            safecode: code,
            date_start: startDate,
            date_end: endDate,
            children: children && children.length ? JSON.stringify(children) : ''
          })
        } else {
          data = qs.stringify({
            username: name,
            pwd: psw,
            safecode: code,
            date_start: startDate,
            date_end: endDate
          })
        }
        http({
          method: 'post',
          url,
          withCredentials: false,
          data
        }).then(res => {
          if (this.checkSuccess(res)) {
            resolve(res.data.data)
          } else {
            reject(res && res.data && res.data.message)
          }
        }).catch(e => {
          console.error('Report Error: ', e)
          reject(e)
        })
      })
    }
  }
}
